﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using DG.Tweening;


namespace linkouqs.Gamekit {

    public sealed class CharacterAbilityThrow : CharacterAbility {

        CharacterAbilityTake m_Take;

        protected override void Awake() {
            base.Awake();

            m_Take = GetComponent<CharacterAbilityTake>();
        }

        public override bool CanStartAction() {

            if (IsInAnimation) {
                return false;
            }

            if(m_Take.ThrowableObject == null) {
                return false;
            }

            if (m_Take.IsInAnimation) {
                return false;
            }

            return true;
        }

        public override void StartAction() {
            base.StartAction();
            Character.Animator.SetTrigger("Throw");
            PlayThrowAnimation();
        }

        void PlayThrowAnimation() {

            if (m_Take.ThrowableObject == null) {
                throw new System.Exception("播放抛石头动画失败");
            }

            Character.CharacterSlots.Detach(m_Take.ThrowableObject.SlotName);
            var obj = m_Take.ThrowableObject;

            var objTrans = obj.transform;

            var dir = (Character.IsLeft ? Vector2.left : Vector2.right) + Vector2.down * 0.3f;
            Vector2 pos = objTrans.position;
            var to = dir * obj.throwDistance + pos;

            m_Take.OnUnselect(obj);

            objTrans.DOMove(to, obj.throwDuraction).SetEase(Ease.OutCubic).OnComplete(()=> {
                if (obj.gameObject.activeSelf) {
                    obj.OnBroken();
                }
                StopAction();
            });
            obj.OnThrow();

        }

    }

}
